package com.wangsd.web.coupon.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.enums.ResultCode;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.Product;
import com.wangsd.web.coupon.service.ProductService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wangsd.common.enums.ResultCode.PRARM_NOT_NULL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-25
 */
@Api(value = "product", tags = "商品管理")
@RestController
@RequestMapping("/coupon/product")
public class ProductController extends MyController {

    @Autowired
    ProductService productService;

    @GetMapping("/selectById/{id}")
    @ApiOperation(value = "查询单条商品信息", notes = "查询单条商品信息", httpMethod = "GET", response = Result.class)
    public Result selectById(@PathVariable String id) {
        Product byId = productService.getById(id);
        return Result.success(byId);
    }

    @PostMapping("/selectList/{pageNum}/{pageSize}")
    @ApiOperation(value = "查询商品列表", notes = "查询商品列表", httpMethod = "POST")
    public Result selectList(@PathVariable Integer pageNum,
                             @PathVariable Integer pageSize,
                             @RequestBody @ApiParam(name = "product", value = "传入json格式") Product product) {
        SysUser loginUser = getUserInfo();
        String code = loginUser.getCode();
        if (product.getCode() != null) {
            code = product.getCode();
        }
        IPage<Product> resultList = productService.selectPage(code, pageNum, pageSize, product);
        return Result.success(resultList);
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除商品", notes = "删除商品", httpMethod = "GET")
    public Result delete(@PathVariable String id) {
        productService.removeById(id);
        return Result.success();
    }


    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "添加或修改商品", notes = "添加或修改商品", httpMethod = "POST")
    public Result addOrUpdate(@RequestBody Product product) {

        if (product.getCompanyId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "商圈（公司）" + PRARM_NOT_NULL.getMessage());
        }
        if (product.getName() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "名称" + PRARM_NOT_NULL.getMessage());
        }
        if (product.getValue() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "金额" + PRARM_NOT_NULL.getMessage());
        }
        if (product.getStoreId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "门店" + PRARM_NOT_NULL.getMessage());
        }
        if (product.getBeginTime() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "开始时间" + PRARM_NOT_NULL.getMessage());
        }
        if (product.getEndTime() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "结束" + PRARM_NOT_NULL.getMessage());
        }
        if (product.getProductPicUrl() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "商品图片" + PRARM_NOT_NULL.getMessage());
        }

        Product oldProduct = productService.selectRepeat(product.getName(), product.getId());
        if (oldProduct != null) {
            return Result.message(ResultCode.NAME_REPEAT);
        }
        productService.addOrUpdate(product);
        return Result.success();
    }

    @ApiOperation(value = "商品上下架", notes = "商品上下架", httpMethod = "POST")
    @PostMapping("/updateState")
    public Result updateState(@RequestBody Product product) {
        if (product.getId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "id" + PRARM_NOT_NULL.getMessage());
        }
        if (product.getState() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "state" + PRARM_NOT_NULL.getMessage());
        }
        productService.updateState(product);
        return Result.success();
    }

    @ApiOperation(value = "商品排序", notes = "商品排序", httpMethod = "POST")
    @PostMapping("/updateSort")
    public Result updateSort(@RequestBody Product product) {
        if (product.getId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "id" + PRARM_NOT_NULL.getMessage());
        }
        if (product.getSort() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "sort" + PRARM_NOT_NULL.getMessage());
        }
        productService.updateById(product);
        return Result.success();
    }

    @ApiOperation(value = "通过商圈id查询商品列表信息--（营销配置使用）", notes = "通过商圈id查询商品列表信息--（营销配置使用）", httpMethod = "POST")
    @PostMapping("/selectProductListByCompanyId")
    public Result selectProductListByCompanyId(@RequestBody Product product) {
        List<Product> products = productService.selectProductListByCompanyId(product);
        return Result.success(products);
    }

}

